﻿using System.IO;
using UnityEngine;
using UnityEditor;
using Framework.Configuration;
using Newtonsoft.Json;

public class FrameworkDll : EditorWindow
{
    #region SyncFrameworkDll
    [MenuItem("EditorTools/SyncFrameworkDll")]
    public static void SyncFrameworkDll()
    {
        string sourcePath = Application.dataPath.Replace("Assets", "Library/ScriptAssemblies/");
        string destPath = Application.dataPath + "/Plugins/Framework/";

        CopyFile(sourcePath, destPath, "Framework.dll");
        CopyFile(sourcePath, destPath, "Framework.pdb");
        CopyFile(sourcePath, destPath, "FrameworkEditor.dll");
        CopyFile(sourcePath, destPath, "FrameworkEditor.pdb");
        AssetDatabase.Refresh();

        EditorApplication.delayCall += SyncDllGuid;
    }

    private static void CopyFile(string pSourcePath, string pDestPath, string pFileName)
    {
        string fullSourcePath = pSourcePath + pFileName;
        string fullDestPath = pDestPath + pFileName;

        if (File.Exists(fullSourcePath))
        {
            File.Copy(fullSourcePath, fullDestPath, true);
            Debug.Log("Sync framework dll succeed. Full dest path:" + fullDestPath);
        }
    }

    private static void SyncDllGuid()
    {
        string jsonPath = Application.dataPath + "/Resources/FrameworkConfig.json";
        if (!File.Exists(jsonPath))
            SaveDllGuid();
        else
            ModifyDllGuid();
    }

    private static void SaveDllGuid()
    {
        string frameworkDllGuid = AssetDatabase.AssetPathToGUID("Assets/Plugins/Framework/Framework.dll", AssetPathToGUIDOptions.OnlyExistingAssets);
        string FrameworkEditorDllGuid = AssetDatabase.AssetPathToGUID("Assets/Plugins/Framework/FrameworkEditor.dll", AssetPathToGUIDOptions.OnlyExistingAssets);
        if (!string.IsNullOrEmpty(frameworkDllGuid) && !string.IsNullOrEmpty(FrameworkEditorDllGuid))
        {
            FrameworkConfiguration frameworkConfiguration = new FrameworkConfiguration();
            frameworkConfiguration.FrameworkDllGuid = frameworkDllGuid;
            frameworkConfiguration.FrameworkEditorDllGuid = FrameworkEditorDllGuid;

            string jsonPath = Application.dataPath + "/Resources/FrameworkConfiguration.json";
            string json = JsonConvert.SerializeObject(frameworkConfiguration);
            File.WriteAllText(jsonPath, json);
            AssetDatabase.Refresh();
        }
    }

    private static void ModifyDllGuid()
    {
        string jsonPath = Application.dataPath + "/Resources/FrameworkConfiguration.json";
        string json = File.ReadAllText(jsonPath);
        FrameworkConfiguration frameworkConfig = JsonConvert.DeserializeObject<FrameworkConfiguration>(json);

        string frameworkDllPath = "Assets/Plugins/Framework/Framework.dll";
        string frameworkDllMetaPath = frameworkDllPath + ".meta";
        FileInfo frameworkDllMeta = new FileInfo(frameworkDllMetaPath);
        if (frameworkDllMeta.Exists)
        {
            string frameworkDllGuid = AssetDatabase.AssetPathToGUID(frameworkDllPath, AssetPathToGUIDOptions.OnlyExistingAssets);
            string metaData = File.ReadAllText(frameworkDllMetaPath);
            metaData = metaData.Replace(frameworkDllGuid, frameworkConfig.FrameworkDllGuid);
            File.WriteAllText(frameworkDllMetaPath, metaData);
        }

        string frameworkEditorDllPath = "Assets/Plugins/Framework/FrameworkEditor.dll";
        string frameworkEditorDllMetaPath = frameworkDllPath + ".meta";
        FileInfo frameworkEditorDllMeta = new FileInfo(frameworkDllMetaPath);
        if (frameworkEditorDllMeta.Exists)
        {
            string frameworkEditorDllGuid = AssetDatabase.AssetPathToGUID(frameworkEditorDllPath, AssetPathToGUIDOptions.OnlyExistingAssets);
            string metaData = File.ReadAllText(frameworkEditorDllMetaPath);
            metaData = metaData.Replace(frameworkEditorDllGuid, frameworkConfig.FrameworkEditorDllGuid);
            File.WriteAllText(frameworkEditorDllMetaPath, metaData);
        }

        AssetDatabase.Refresh();
    }
    #endregion

    [MenuItem("EditorTools/DeleteFrameworkDll")]
    public static void DeleteFrameworkDll()
    {
        string destPath = Application.dataPath + "/Plugins/Framework/";

        File.Delete(string.Format("{0}/{1}", destPath, "Framework.dll"));
        File.Delete(string.Format("{0}/{1}", destPath, "Framework.pdb"));
        File.Delete(string.Format("{0}/{1}", destPath, "FrameworkEditor.dll"));
        File.Delete(string.Format("{0}/{1}", destPath, "FrameworkEditor.pdb"));
        Debug.LogError("Framework dll delete succeed.");
        AssetDatabase.Refresh();
    }
}